﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Microsoft.SharePoint;
using System.IO;
using System.Web.Script.Serialization;

namespace ConsolidationTool.Common
{
    public class ForecastData
    {
        /// <summary>
        /// 获取Actual数据
        /// </summary>
        /// <param name="coCode">Company code </param>
        /// <param name="year">Year</param>
        /// <param name="month">Month</param>
        /// <param name="listName">数据表</param>
        /// <returns></returns>
        public static DataTable GetActualDt(string coCode, string year, int month, string listName)
        {
            DataTable newdt = new DataTable();
            for (int i = 1; i <= month; i++)
            {
                object[] obj = new object[newdt.Columns.Count];
                SPListItem actual = Common.BusinessService.GetItem(coCode, year, i.ToString(), listName);
                if (actual != null)
                {
                    DataTable dt = actual.ListItems.GetDataTable();
                    if (newdt.Rows.Count > 0)
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            dt.Rows[j].ItemArray.CopyTo(obj, 0);
                            newdt.Rows.Add(obj);
                        }
                    }
                    else
                        newdt = dt.Copy();
                }
            }
            return newdt;
        }

        /// <summary>
        /// 读取列名
        /// </summary>
        /// <param name="url">路径</param>
        /// <returns></returns>
        public static List<FileEntity> GetName(string url)
        {
            using (StreamReader sr = new StreamReader(url))
            {
                string str = sr.ReadToEnd();
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                var list = serializer.Deserialize<List<FileEntity>>(str);
                return list;
            }
        }

        /// <summary>
        /// 获取Forecast数据
        /// </summary>
        /// <param name="coCode">Company code </param>
        /// <param name="year">Year</param>
        /// <param name="month">Month</param>
        /// <param name="listName">数据表</param>
        /// <param name="dcname">列名</param>
        /// <returns>HR01-HR04</returns>
        public static string GetForecast(string coCode, string year, string month, string listName, string dcname, string type, out List<fssubentity> subForecast)
        {
            string str = "";
            List<fssubentity> subrow = null;
            SPListItem forecast = Common.BusinessService.GetItem(coCode, year, month, listName);
            if (forecast != null)
            {
                dcname = dcname.Replace("CM_", string.Empty);

                string billData = forecast["BillData"].ToString();
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                var objentity = serializer.Deserialize<List<fsentity>>(billData);
                objentity.ForEach(p =>
                {
                    if (p.accountcode != null)
                    {
                        if (p.accountcode.Trim() == dcname.Replace(dcname.Substring(0, 4), dcname.Substring(0, 2) + "04"))
                        {
                            if (type == "HR" || type == "CAPEX" || type == "DCF" || type == "PL"||type=="SCS")
                            {
                                str = "<td>" + p.M_1 + "</td><td>" + p.M_2 + "</td><td>" + p.M_3 + "</td>";
                            }
                            else if (type == "TSBR" || type == "PSBR")
                            {
                                str = "<td>" + p.M_1_Qty + "</td><td>" + p.M_1_T + "</td>";
                                str += "<td>" + p.M_2_Qty + "</td><td>" + p.M_2_T + "</td>";
                                str += "<td>" + p.M_3_Qty + "</td><td>" + p.M_3_T + "</td>";
                            }
                            else if (type == "SID")
                            {
                                str = "<td>" + p.M_1_Qty + "</td><td></td><td></td><td>" + p.M_1_BA + "</td>";
                                str += "<td>" + p.M_2_Qty + "</td><td></td><td></td><td>" + p.M_2_BA + "</td>";
                                str += "<td>" + p.M_3_Qty + "</td><td></td><td></td><td>" + p.M_3_BA + "</td>";
                            }
                            subrow = p.subrow;
                        }
                    }
                });
            }
            subForecast = subrow;
            return str;
        }


        /// <summary>
        /// 获取子项数据
        /// </summary>
        /// <param name="coCode">公司code</param>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <param name="listName">子项数据源</param>
        /// <param name="accountCode">父类code</param>
        /// <returns></returns>
        public static DataTable GetSubActualDs(string coCode, string year, int month, string listName, string accountCode, string type)
        {
            DataSet ds = new DataSet();
            DataTable newdt = new DataTable();
            newdt.Columns.Add("RGC");
            newdt.Columns.Add("CoName");
            for (int i = 1; i <= month; i++)
            {
                newdt.Columns.Add("Qty" + i);
                if (type == "SID")
                {
                    newdt.Columns.Add("IC" + i);
                    newdt.Columns.Add("IA" + i);
                    newdt.Columns.Add("BA" + i);
                }
                else
                    newdt.Columns.Add("Turnover" + i);
            }
            for (int i = 1; i <= month; i++)
            {
                SPListItemCollection subactual = Common.BusinessService.GetSubItems(coCode, year, i.ToString(), listName, accountCode);
                if (subactual != null)
                {
                    DataTable dt = subactual.GetDataTable();
                    if (dt != null)
                    {
                        for (int j = 0; j < dt.Rows.Count; j++)
                        {
                            bool check = true;
                            string rgc = dt.Rows[j]["RGC"].ToString();
                            int index = 0;
                            for (int m = 0; m < newdt.Rows.Count; m++)
                            {
                                if (newdt.Rows[m]["RGC"].ToString() == rgc)
                                {
                                    check = false;
                                    index = m;
                                }
                            }
                            if (check)
                            {
                                DataRow newdr = newdt.NewRow();
                                newdr["RGC"] = dt.Rows[j]["RGC"];
                                newdr["CoName"] = dt.Rows[j]["CoName"];
                                newdr["Qty" + i] = dt.Rows[j]["CM_QTY_" + accountCode];
                                if (type == "SID")
                                {
                                    newdr["IC" + i] = dt.Rows[j]["CM_IC_" + accountCode];
                                    newdr["IA" + i] = dt.Rows[j]["CM_IA_" + accountCode];
                                    newdr["BA" + i] = dt.Rows[j]["CM_BA_" + accountCode];
                                }
                                else
                                    newdr["Turnover" + i] = dt.Rows[j]["CM_T_" + accountCode];
                                newdt.Rows.Add(newdr);
                            }
                            else
                            {
                                newdt.Rows[index]["Qty" + i] = dt.Rows[j]["CM_QTY_" + accountCode];
                                if (type == "SID")
                                {
                                    newdt.Rows[index]["IC" + i] = dt.Rows[j]["CM_IC_" + accountCode];
                                    newdt.Rows[index]["IA" + i] = dt.Rows[j]["CM_IA_" + accountCode];
                                    newdt.Rows[index]["BA" + i] = dt.Rows[j]["CM_BA_" + accountCode];
                                }
                                else
                                    newdt.Rows[index]["Turnover" + i] = dt.Rows[j]["CM_T_" + accountCode];
                            }
                        }
                    }
                }
            }
            return newdt;
        }

        [Serializable]
        public class FileEntity
        {
            public string code { get; set; }
            public string desc { get; set; }
        }
    }
}
